/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.dao.mock;

import java.lang.reflect.Array;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.activequant.dao.IDaoSupport;
import org.activequant.util.exceptions.DaoException;
import org.apache.log4j.Logger;


/**
 * @TODO desc<br>
 * <br>
 * <b>History:</b><br>
 *  - [26.10.2007] Created based on Miks DaoMarketDataEntityBase code (Erik Nijkamp)<br>
 *  - [27.10.2007] Replaced compareByExample(...) with T.equals() (Erik Nijkamp)<br>
 *  - [27.10.2007] Removed dirty setId()/getId() code (Erik Nijkamp)<br>
 *  - [31.10.2007] Renamed class to match file (Ulrich Staudinger)<br>
 *  - [02.11.2007] existing ids in update method are not overwritten any more (Ulrich Staudinger)<br>
 *
 *  @author Erik Nijkamp
 *  @author Mike Kroutikov
 *  @author Ulrich Staudinger
 */
abstract class MockDaoSupportBase<T> implements IDaoSupport<T> {
		
	final Logger log = Logger.getLogger(getClass());
	
	protected final Map<T,T> database = new TreeMap<T,T>();
	protected final Map<Long,T> index = new TreeMap<Long,T>();
	private long id = 0;
	private Class<T> clazz;
	
	abstract Long getId(T t);
	abstract void setId(T t, Long id);
	
	protected boolean compareByExample(T model, T sample) {
		return model.equals(sample);
	}
	
	protected MockDaoSupportBase(Class<T> clazz) {
		this.clazz = clazz;
	}
	
	protected Long generateId() {
		return new Long(++id);
	}
	
	@SuppressWarnings("unchecked")
	protected T[] createArray(int length) {
		return (T[])Array.newInstance(clazz, length);
	}
	
	public void delete(T entity) throws DaoException {

		log.info("removing: " + entity);
		
		T value = database.remove(entity);
		if(value != null) {
			index.remove(getId(value));
			log.info("successfully removed: " + value);
		} else {
			log.info("can not remove (not found): " + entity);
		}
	}

	public void delete(T... entities) throws DaoException {
		for(T t : entities) {
			delete(t);
		}
	}

	public void delete(List<T> entities) throws DaoException {
		for(T t : entities) {
			delete(t);
		}
	}
	
	public T update(T entity) throws DaoException {
		T t = findByExample(entity);
		if(t == null) {
			Long id = generateId();
			// safety to not overwrite existing ids. 
			if(getId(entity)==null)setId(entity, id);
			database.put(entity, entity);
			index.put(id, entity);
			return entity;
		}
		
		return t;
	}

	public T[] update(T... entities) throws DaoException {
		List<T> out = new LinkedList<T>();
		for(T t : entities) {
			out.add(update(t));
		}
		return out.toArray(entities);
	}

	public List<T> update(List<T> entities) throws DaoException {
		List<T> out = new LinkedList<T>();
		for(T t : entities) {
			out.add(update(t));
		}
		return out;
	}

	public int count() throws DaoException {
		return database.values().size();
	}

	public void deleteAll() throws DaoException {
		database.clear();
		index.clear();
	}

	public T find(long id) throws DaoException {
		return index.get(id);
	}

	public T[] findAll() throws DaoException {
		return database.values().toArray(createArray(0));
	}
	
	public T[] findAllByExample(T entity) throws DaoException {
		List<T> out = new LinkedList<T>();
		for(T t : database.values()) {
			if(compareByExample(entity, t)) {
				out.add(t);
			}
		}
		
		return out.toArray(createArray(0));
	}
	
	public T findByExample(T entity) throws DaoException {

		for(T t : database.values()) {
			if(compareByExample(entity, t)) {
				return t;
			}
		}
		
		return null;
	}
}
